package com.pxst.fi.service.impl;

import com.pxst.et.entity.SysEtOrderEntity;
import com.pxst.fi.entity.SysFiOrderEntity;
import com.pxst.fi.entity.SysLogLastPageEntity;
import com.pxst.fi.service.SysLogLastPageService;
import com.pxst.fi.mapper.SysLogLastPageMapper;
import com.pxst.fo.sys.entity.SysFoOrderChildEntity;
import com.pxst.fx.entity.SysFxOrderChildEntity;
import com.pxst.internalReports.resp.LogLastPageResp;
import com.pxst.resp.ApiResp;
import com.pxst.tr.entity.SysIntroversionOrderEntity;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author notch
 * @description 针对表【sys_log_last_page(Log Last Page（LL100）)】的数据库操作Service实现
 * @createDate 2023-09-18 15:25:50
 */
@Service
public class SysLogLastPageServiceImpl extends JoinServiceImpl<SysLogLastPageMapper, SysLogLastPageEntity>
        implements SysLogLastPageService {

    @Resource
    private SysLogLastPageMapper logLastPageMapper;

    @Override
    public ApiResp searchLogLastPage(String commandId) {

        /**
         * 根据不同的commandId进行不同的连表查询,然后返回结果集
         */

        JoinLambdaWrapper<SysLogLastPageEntity> jlw = new JoinLambdaWrapper<>(SysLogLastPageEntity.class);
        jlw
                .eq(commandId != null, SysLogLastPageEntity::getCommandId, commandId)
                .selectAs(llp -> {
                    llp.addFunAlias(SysLogLastPageEntity::getId, LogLastPageResp::getLogLastPageId);
                    llp.addFunAlias(SysLogLastPageEntity::getCommandId, LogLastPageResp::getCommandId);
                    llp.addFunAlias(SysLogLastPageEntity::getData, LogLastPageResp::getData);
                    llp.addFunAlias(SysLogLastPageEntity::getMessage, LogLastPageResp::getMessage);
                    llp.addFunAlias(SysLogLastPageEntity::getCreatedAt, LogLastPageResp::getCreatedAt);
                });
        if (commandId.contains("FI") || commandId.contains("fi")) {
            jlw.leftJoin(SysFiOrderEntity.class, SysFiOrderEntity::getCommandId, SysLogLastPageEntity::getCommandId)
                    .end();
        }

        if (commandId.contains("FO") || commandId.contains("fo")) {
            jlw.leftJoin(SysFoOrderChildEntity.class, SysFoOrderChildEntity::getCommandId, SysLogLastPageEntity::getCommandId)
                    .end();
        }

        if (commandId.contains("FX") || commandId.contains("fx")) {
            jlw.leftJoin(SysFxOrderChildEntity.class, SysFxOrderChildEntity::getCommandId, SysLogLastPageEntity::getCommandId)
                    .end();
        }

        if (commandId.contains("TR") || commandId.contains("tr")) {
            jlw.leftJoin(SysIntroversionOrderEntity.class, SysIntroversionOrderEntity::getCommandId, SysLogLastPageEntity::getCommandId)
                    .end();
        }

        if (commandId.contains("ET") || commandId.contains("et")) {
            jlw.leftJoin(SysEtOrderEntity.class, SysEtOrderEntity::getCommandId, SysLogLastPageEntity::getCommandId)
                    .end();
        }

        List<LogLastPageResp> resps = logLastPageMapper.joinSelectList(jlw, LogLastPageResp.class);

        return ApiResp.sucess(resps);
    }
}




